package com.bosssoft.cart.controller;

import com.alibaba.fastjson.JSON;
import com.bosssoft.cart.mapper.BookMapper;
import com.bosssoft.cart.pojo.Books;
import com.bosssoft.cart.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BookController {

    @Autowired
    private BookService bookService;

    /**
     * 获取商品列表
     * @return 商品列表
     */
    @GetMapping("/booklist")
    public String queryAllBooks(){
        List<Books> list = bookService.queryAllBook();
        return JSON.toJSONString(list);
    }

    /**
     * 添加商品
     * @param books 商品
     */
    @PostMapping("/add")
    public void addBook(Books books){
        bookService.addBook(books);
    }

    /**
     * 根据id删除商品
     * @param bookID 商品id
     */
    @DeleteMapping("/delete/{id}")
    public void deleteBook(int bookID){
        bookService.deleteBookById(bookID);
    }

    /**
     * 根据id获取商品
     * @param bookID 商品id
     * @return 商品
     */

    @GetMapping("/query/{id}")
    public String queryBookById(int bookID){
        Books books = bookService.queryBookById(bookID);
        return JSON.toJSONString(books);
    }

    /**
     * 更新商品
     * @param books 商品books
     */
    @PostMapping("/update")
    public void updateBook(Books books){
        bookService.uodateBook(books);
    }
}
